package com.isay.frameworklib.utils;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.io.InputStream;

/**
 * @author yfren
 */
public class ViewUtils {

    public static Rect getViewRectOnScreen(View child) {
        Rect viewRect = new Rect();
        if (child == null)
            return viewRect;
        int[] childPosition = new int[2];
        child.getLocationOnScreen(childPosition);
        int left = childPosition[0];
        int right = left + child.getWidth();
        int top = childPosition[1];
        int bottom = top + child.getHeight();
        viewRect.set(left, top, right, bottom);
        return viewRect;
    }

    public static boolean isEventOnView(MotionEvent e, View child) {
        if (e == null || child == null)
            return false;
        Rect viewRect = getViewRectOnScreen(child);
        return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
    }

    public static Bitmap viewToBitmap(View view) {
        Bitmap bitmap = null;
        if (view == null || !(view.getWidth() > 0 && view.getHeight() > 0))
            return bitmap;

        bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
                Bitmap.Config.RGB_565);
        // 利用bitmap生成画布
        Canvas canvas = new Canvas(bitmap);
        // 把view中的内容绘制在画布上
        view.draw(canvas);
        return bitmap;
    }

    // public static Bitmap viewToBitmap(View view,float scale) {
    // Bitmap bitmap = null;
    // if (view == null)
    // return bitmap;
    // bitmap =
    // Bitmap.createBitmap((int)(view.getWidth()*scale),(int)(view.getHeight()*scale),
    // Bitmap.Config.RGB_565);
    // // 利用bitmap生成画布
    // Canvas canvas = new Canvas(bitmap);
    // // 把view中的内容绘制在画布上
    // view.draw(canvas);
    // return bitmap;
    // }

    // public static Bitmap viewToBitmap(View view) {
    // Bitmap bitmap = null;
    // if (view == null)
    // return bitmap;
    // // 开启view绘图缓存
    // view.setDrawingCacheEnabled(true);
    // view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
    // MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    // view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    //
    // view.buildDrawingCache();
    // // 获取mDragItemView在缓存中的Bitmap对象
    // bitmap = Bitmap.createBitmap(view.getDrawingCache());
    // // 这一步很关键，释放绘图缓存，避免出现重复的镜像
    // view.destroyDrawingCache();
    // return bitmap;
    // }

    @SuppressWarnings("deprecation")
    public static Bitmap activityToBitmap(Activity activity) {
        if (activity == null)
            return null;
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;

        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay()
                .getHeight();

        Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width,
                height - statusBarHeight);
        view.destroyDrawingCache();
        return bitmap2;
    }

    /**
     * 设置自定义shape drawable 会不显示
     *
     * @param view
     * @param resId
     */
    @SuppressWarnings("deprecation")
    public static void setViewBackground(View view, int resId) {
        if (view == null)
            return;
        BitmapDrawable bitmapDrawable = getBitmapDrawable(view.getContext(),
                resId);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(bitmapDrawable);
        } else {
            view.setBackgroundDrawable(bitmapDrawable);
        }
    }


    public static void setViewBackground(View view, Bitmap bitmap) {
        if (bitmap != null) {
            Drawable drawable = new BitmapDrawable(bitmap);
            view.setBackgroundDrawable(drawable);
        }
    }


    public static void setImageViewSrc(Context context, ImageView view,
                                       int resId) {
        if (context == null || view == null)
            return;
        Bitmap b = getBitmap(context, resId);
        view.setImageBitmap(b);
    }

    @SuppressWarnings("deprecation")
    public static void setViewBackground(int resId, View view) {
        if (view == null)
            return;
        Drawable d = view.getContext().getResources().getDrawable(resId);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(d);
        } else {
            view.setBackgroundDrawable(d);
        }
    }


    /**
     * 设置src,RGB_565格式
     *
     * @param view
     * @param resId
     */
    public static void setImageViewSrc(ImageView view, int resId) {
        if (view == null)
            return;
        Bitmap b = getBitmap(view.getContext(), resId);
        view.setImageBitmap(b);
    }

    /**
     * 返回RGB_565 BitmapDrawable
     *
     * @param context
     * @param resId
     * @return
     */
    public static BitmapDrawable getBitmapDrawable(Context context, int resId) {
        BitmapDrawable bitmapDrawable = null;
        if (context == null)
            return bitmapDrawable;
        Bitmap bitmap = getBitmap(context, resId);
        bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
        return bitmapDrawable;
    }

    /**
     * 根据id获取bitmap
     *
     * @param @param context
     * @param @param resId
     * @return Bitmap 返回类型
     * @throws
     * @Title: getBitmap
     */
    private static Bitmap getBitmap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;

        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

    // 增加绑定界面文件View
    public static View getView(Context context, int resId) {
        return getView(context, resId, null);
    }

    public static View getView(Context context, int resId, ViewGroup root) {
        View nv = null;
        try {
            nv = View.inflate(context, resId, root);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return nv;
    }

    public static View setOnClickListener(View v, int id, View.OnClickListener l) {
        if (v == null)
            return null;

        View child = v.findViewById(id);
        if (child != null) {
            child.setOnClickListener(l);
        }

        return child;
    }

    public static View setOnClickListener(Activity act, int id,
                                          View.OnClickListener l) {
        if (act == null)
            return null;

        View child = act.findViewById(id);
        if (child != null) {
            child.setOnClickListener(l);
        }

        return child;
    }



}
